<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/7/14
 * Time: 17:27
 */
use app\assets\AppAsset;
use yii\helpers\Html;

AppAsset::register($this);

$baseUrl = Yii::$app->request->baseUrl;
AppAsset::addCss($this, $baseUrl . "/css/my-address.css");

$this->title = '我的地址';
?>
<div style="padding-left: 15px;padding-right: 15px;">
    <?php for($i=0;$i<count($addresslist);$i++){?>
        <div class="commonDiv">
            <div class="userDetailDiv" onclick="setDefault(<?= $addresslist[$i]['addressbook_id']?>)">
                <span id="tvName"><?=$addresslist[$i]['fullname']?></span>
                <span id="tvPhone"><?=$addresslist[$i]['mobile']?></span>
            </div>

            <div class="addressContent" style="margin-top: 10px" onclick="setDefault(<?= $addresslist[$i]['addressbook_id']?>)">
            <span id="tvContent"><?=$addresslist[$i]['province'] .
                $addresslist[$i]['city'] . $addresslist[$i]['district'] .
                $addresslist[$i]['address']?></span>
            </div>

            <div >
                <?php if($addresslist[$i]['is_default'] == 1){?>
                    <div class="setDiv">默认地址</div>
                    <div class="deleteDiv" onclick="deleteAddress(<?= $addresslist[$i]['addressbook_id']?>)">删除</div>
                <?php }else {?>
                    <div class="setDiv" onclick="setDefault(<?= $addresslist[$i]['addressbook_id']?>)">设为默认地址</div>
                    <div class="deleteDiv" onclick="deleteAddress(<?= $addresslist[$i]['addressbook_id']?>)">删除</div>
                <?php }?>

                <div style="clear: both"></div>
            </div>
            <div class="lineDiv"></div>
        </div>
    <?php }?>

    <div style="height: 45px"></div>
    <footer >
        <div class="addAddressDiv" onclick="goToAddAddress()">添加新地址</div>
    </footer>
</div>



<script >
    function goToAddAddress(){
        location.href=("new-address");
    }

    function setDefault(addressbook_id){
        $("#")
        $.ajax({
//            type:"POST",
//            dataType:"json",
            data:{"addressbook_id":addressbook_id},
            url:"default",
            success:function(json){
//                console.log("123",json);
//                alert("设置成功");
                  window.history.back();
            },
            error:function(data){
//                console.log(data);
                alert("设置失败");
            }
        });
    }

    function deleteAddress(addressbook_id){
        $.ajax({
//            type:"POST",
//            dataType:"json",
            data:{"addressbook_id":addressbook_id},
            url:"delete-address",
            success:function(json){
//                console.log("123",json);
                alert("删除成功");
                location.reload();
            },
            error:function(data){
//                console.log(data);
                alert("删除失败");
            }
        });
    }
</script>
